package com.bolins.util;

import java.text.DecimalFormat;

public class FormatNumber {
	private final static String formatString = "######.##";
	private final static String formatString2 = "######.##";

	/**
	 * 将一个Double数转换为一个###,###.##格式的字符串
	 * 
	 * @param num
	 * @return
	 */
	public static String formatNumberToString(double num) {
		DecimalFormat dformat = new DecimalFormat(formatString);
		return dformat.format(num);
	}

	/**
	 * 将一个Double数精确小数点后二位
	 * 
	 * @param num
	 * @return 精确三位的Double数,如果传进来的不是一个Double,返回num
	 */
	public static Double formatNumberToDouble(double num) {
		DecimalFormat dformat = new DecimalFormat(formatString2);
		double result = num;
		try {
			result = Double.parseDouble(dformat.format(num));
		} catch (NumberFormatException e) {
			e.printStackTrace();
		}
		return result;
	}
}
